package com.yamvc.net.media
{
	import com.yamvc.mvc.controller.Controller;
	import com.yamvc.net.media.events.WowzaConnectorEvent;
	
	import flash.events.AsyncErrorEvent;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	
	public class WowzaConnector extends EventDispatcher
	{
		protected var _netConnection:NetConnection;
		protected var _streams:ArrayCollection;
		protected var _callResponder:Responder;
		
		public function WowzaConnector()
		{
			_netConnection = new NetConnection();
			_streams = new ArrayCollection();
			_callResponder = new Responder(callResultHandler,callStatusHandler);
			initListeners();
		}
		
		protected function initListeners():void{
			_netConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,netConnection_asyncErrorHandler);
			_netConnection.addEventListener(IOErrorEvent.IO_ERROR,netConnection_IOErrorHandler);
			_netConnection.addEventListener(NetStatusEvent.NET_STATUS,netConnection_netStatusHandler);
			_netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,netConnection_securityErrorHandler);
		}
		
		public function authConnect(uri:String,userName:String,password:String):void{
			if(!_netConnection.connected){
				_netConnection.connect(uri,userName,password);
			}
		}
		
		public function connect(uri:String):void{
			_netConnection.connect(uri);
		}
		
		public function createStream():NetStream{
			var ns:NetStream = new NetStream(_netConnection);
			ns.client=this;
			return ns;
		}
		public function onMetaData(param:Object):void{
			trace(param);
		}
		
		public function close():void{
			_netConnection.close();
			Controller.instance.dispatchEvent(new WowzaConnectorEvent(WowzaConnectorEvent.CONNECTION_CLOSED));
		}
		public function set client(value:Object):void{
			_netConnection.client=value;
		}
		
		private var _resultReceaved:Boolean=false;
		private var _resultObj:Object;
		public function get resultObject():Object{
			return _resultObj;
		}
		public function call(functionName:String,...params):void{
			_resultReceaved = false;
			_resultObj = null;
			var args:Array = [functionName,_callResponder];
			args=args.concat(params);
			trace(params.join(">>"));
			trace(args.join(""));
			_netConnection.call.apply(_netConnection,args);
		}
		
		private function callResultHandler(result:Object):void{
			_resultReceaved=true;
			_resultObj = result;
		}
		private function callStatusHandler():void{
			
		}
		//////// PRIVATE
		private function netConnection_asyncErrorHandler(e:AsyncErrorEvent):void{
		 trace(e.text);
		}
		
		private function netConnection_IOErrorHandler(e:IOErrorEvent):void{
			
		}
		
		private function netConnection_netStatusHandler(e:NetStatusEvent):void{
			trace(e.info.code);
			if(e.info.code=="NetConnection.Connect.Success"){
				dispatchEvent(new WowzaConnectorEvent(WowzaConnectorEvent.CONNECT));
			}else if(e.info.code=="NetConnection.Connect.Rejected"){
				dispatchEvent(new WowzaConnectorEvent(WowzaConnectorEvent.REJECT,e.info.application));
			}else if(e.info.code=="NetConnection.Connect.Failed"){
				dispatchEvent(new WowzaConnectorEvent(WowzaConnectorEvent.FAILED));
			}
		}
		
		private function netConnection_securityErrorHandler(e:SecurityErrorEvent):void{
			
		}

	}
}